package com.example.kotlinExample.service;

import com.example.kotlinExample.entity.User;
import com.example.kotlinExample.entity.UserInfo;
import com.example.kotlinExample.repository.db.UserInfoRepository;
import com.example.kotlinExample.repository.db.UserRepository;
import com.example.kotlinExample.repository.es.UserEsRepository;
import com.example.kotlinExample.repository.es.UserInfoEsRepository;
import com.example.kotlinExample.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author liliang
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserEsRepository userEsRepository;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private UserInfoEsRepository userInfoEsRepository;


    @Transactional(rollbackFor = Exception.class)
    public User save(User user) {
        user.setCreateTime(new Date());
        User save = userRepository.save(user);
        userEsRepository.save(user);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(save.getId());
        userInfo.setGender(1);
        userInfo.setNickname(user.getName() + "nickname");
        userInfo.setUpdateTime(new Date());
        userInfo.setCreateTime(new Date());
        userInfoRepository.save(userInfo);
        userInfoEsRepository.save(userInfo);
        return save;
    }

    public User getUser(Integer id) {
        Optional<User> optional = userRepository.findById(id);
        return optional.orElse(new User());
    }

    public Page<User> selectUser(String name) {
        PageRequest request = PageRequest.of(0, 2);
        return userEsRepository.findAllByName(name, request);
    }

    public List<User> getUserList() {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        return userRepository.findAll(sort);
//        return userRepository.findAll();
    }

    public Page<User> getPage() {
        PageRequest request = PageRequest.of(0, 2);
        return userRepository.findAll(request);
    }

    public Page<User> search(String keyword, Integer pageNum, Integer pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize, sort);
//        return userRepository.findAll(pageRequest);
        return userRepository.search(keyword, pageRequest);
    }

    public Page<UserVo> searchUserInfo(String keyword, Integer pageNum, Integer pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize, sort);
        return userRepository.searchUserInfo(keyword, pageRequest);
    }
}
